#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int inf = 0x3f3f3f3f;
const int N = 1410;
const int M = 3e7+10;

int h[N],now[N],to[N],out[N],s1,s2,t1,t2,cnt,d[N],n,m;
struct edge{int v,w,nxt;}e[M<<1];
void link(int x,int y,int w){e[++cnt] = (edge){y,w,h[x]};h[x] = cnt;e[++cnt] = (edge){x,0,h[y]};h[y] = cnt;}

bool bfs(){
	queue<int> q; q.push(s1); memset(d,0,sizeof(d)); d[s1] = 1;now[s1] = h[s1];
	while(q.size()){
		int x = q.front(); q.pop();
		for(int i = h[x],y;i;i = e[i].nxt){
			if(!d[y = e[i].v] && e[i].w){
				d[y] = d[x] + 1; now[y] = h[y];
				q.push(y); if(y == t1) return 1;
			}
		}
	}return 0;
}

int dfs(int x,int flow){
	if(x == t1) return flow; int res = flow;
	for(int i = now[x],y;i&&res;i = e[i].nxt){
		now[x] = i;
		if(d[y = e[i].v] == d[x] + 1 && e[i].w){
			int k = dfs(y,min(res,e[i].w));
			if(!k) d[y] = 0; res -= k; e[i].w -= k; e[i^1].w += k;
		}
	}return flow - res;
}

void solve(){
	memset(h,0,sizeof(h)); cnt = 1; memset(to,0,sizeof(to)); memset(out,0,sizeof(out));
	s2 = n+m+1,t2 = n+m+2,s1 = n+m+3,t1 = n+m+4; int sum = 0;
	for(int i = 1;i <= m;i++){int g = in; to[t2] += g; out[i+n] += g; link(i+n,t2,inf-g);}
	for(int i = 1;i <= n;i++){
		int c = in,d = in; link(s2,i,d);
		for(int j = 1;j <= c;j++){
			int t = in+1,l = in,r = in;
			link(i,t+n,r-l); to[t+n] += l; out[i] += l;
		}
	}
	for(int i = 1;i <= n+m+2;i++)
		if(to[i] > out[i]) link(s1,i,to[i] - out[i]),sum += to[i] - out[i];
		else link(i,t1,out[i] - to[i]);
	link(t2,s2,inf);
	int maxflow = 0,flow = 0;
	while(bfs()) while(flow = dfs(s1,inf)) maxflow += flow;
	if(maxflow != sum) return puts("-1\n"),void();
	maxflow = e[cnt].w;
	e[cnt].w = e[cnt^1].w = 0; s1 = s2; t1 = t2;
	while(bfs()) while(flow = dfs(s1,inf)) maxflow += flow;
	return printf("%d\n\n",maxflow),void();
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	while(scanf("%d %d",&n,&m) == 2) solve();
	return 0;
}

